//turret.txt - Simple script for turrets
//Cell 0,1 - Stuff done flag. If both 0, nothing. Otherwise If non-zero, this 
//turret wont attack. If the turret is itself attacked, will set the not-attack setting back to 0.
//Cell 2,3 - Stuff done flag. If both 0, nothing. Otherwise when this is killed, set to 1.

begincreaturescript;

short i,target;
short last_abil;
short adj = 0;
short pillar;
short mode = 0;
short pillar_mes;

body;

beginstate INIT_STATE;
	set_name(ME,"Travald's Advisor");
	set_boss_level(ME,2);
	place_particle_num(ME,6,12,6);
	set_walk_speed(ME,0);
	set_new_abil(ME,20);
	
	set_resistance(ME,0,90);
	set_resistance(ME,1,0);
	set_resistance(ME,2,0);
	
	last_abil = get_current_tick();
	pillar = get_current_tick();
	break;

beginstate DEAD_STATE;
	sf(55,8,1);
	erase_char(29);
	erase_char(30);
	
break;

beginstate START_STATE; 
	if (gf(55,15) > 1)
		end();

	if ((adj == 0) && (gf(55,15) == 1)) {
		adj = 1;
		change_max_health(ME,100);
		set_boss_level(ME,1);
		}
		
	if ((get_attitude(ME) >= 10) && (char_ok(29) == FALSE) && (get_nearest_party_char(9) < 0)) {
		summon_creature(21);
		set_name(29,"Hunter Shade");
		set_level(29,25);
		set_boss_level(29,-1);
		set_attack_bonus(29,6);
		set_summon_level(29,1);
		
		set_resistance(29,0,90);
		}
	if ((get_attitude(ME) >= 10) && (char_ok(30) == FALSE) && (get_nearest_party_char(9) < 0)) {
		summon_creature(22);
		set_name(30,"Hunter Shade");
		set_level(30,25);
		set_boss_level(30,-1);
		set_attack_bonus(30,6);
		set_summon_level(30,1);
		
		set_resistance(30,1,90);
		set_resistance(30,2,90);
		set_resistance(30,3,90);
		}
		
	if (who_shot_me() >= 0) {
		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}

		
	if (get_foe_target(ME,8,0)) {
		do_attack();
		set_state(3);
		}
	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking
	if (gf(55,15) > 1)
		end();
		
	if (target_ok() == FALSE)
		set_state(START_STATE);

	if (tick_difference(pillar,get_current_tick()) > 2) {
		pillar = get_current_tick();

		if (mode == 0) {
			mode = 1;
			set_terrain(52,8,87);
			set_terrain(50,10,87);
			
			set_resistance(ME,0,0);
			set_resistance(ME,1,90);
			set_resistance(ME,2,90);
			}
			else {
				mode = 0;
				set_terrain(52,8,86);
				set_terrain(50,10,86);
			
				set_resistance(ME,0,90);
				set_resistance(ME,1,0);
				set_resistance(ME,2,0);
				}
				
		run_char_animation(2,1,35);	
		pc_heard_sound_delay(240,100); 							
		
		print_str_color("The pillars behind the shade change colors.",2);
		
		if (pillar_mes == 0) {
			pillar_mes = 1;
			begin_talk_mode(104);
			}

		end();
		}
		
	if ((is_combat()) && (can_see_char(get_target())) && (get_target() != pc_num()) && (tick_difference(last_abil,get_current_tick()) > 2)) {
			print_named_str(ME,"points at a creation and mutters a single word.");
		run_char_animation(2,1,35);	
		last_abil = get_current_tick();
			set_char_status(get_target(),5,40);
			pc_heard_sound_delay(243,100);					

		end();
	  	}
	
	if ((get_health(ME) < (get_max_health(ME) * 5) / 6) && (gf(54,20) == 0)) {
		sf(54,20,1);
		print_str_color("The shade waves a hand. A door starts to open.",2);

		set_attitude(48,10);
		set_attitude(49,10);

		run_char_animation(2,1,50);	
		pc_heard_sound_delay(187,100);						
		
		begin_talk_mode(103);
		}
	else if ((get_health(ME) < (get_max_health(ME) * 4) / 6) && (gf(54,21) == 0)) {
		sf(54,21,1);
		print_str_color("The shade waves a hand. A door starts to open.",2);

		set_attitude(50,10);

		run_char_animation(2,1,50);	
		pc_heard_sound_delay(187,100);					
		
		end();
		}
	else if ((get_health(ME) < (get_max_health(ME) * 3) / 6) && (gf(54,22) == 0)) {
		sf(54,22,1);
		print_str_color("The shade waves a hand. A door starts to open.",2);

		set_attitude(51,10);
		set_attitude(52,10);

		run_char_animation(2,1,50);	
		pc_heard_sound_delay(187,100);	 				
		
		end();
		}
	else if ((get_health(ME) < (get_max_health(ME) * 2) / 6) && (gf(54,23) == 0)) {
		sf(54,23,1);
		print_str_color("The shade waves a hand. A door starts to open.",2);

		set_attitude(53,10);

		run_char_animation(2,1,50);	
		pc_heard_sound_delay(187,100);					
		
		end();
		}
	else if ((get_health(ME) < (get_max_health(ME) * 1) / 6) && (gf(54,24) == 0)) {
		sf(54,24,1);
		print_str_color("The shade waves a hand. A door starts to open.",2);

		set_attitude(54,10);
		set_attitude(55,10);

		run_char_animation(2,1,50);	
		pc_heard_sound_delay(187,100);	 					
		
		end();
		}


	do_attack();
	turret_heal();
break;

beginstate TALKING_STATE;
	if (gf(55,15) > 1)
		print_str_color("It can't talk to you. It's too busy crumbling.",2);
		else begin_talk_mode(90);
break;